import Vue from 'vue'
import VeeValidate, { Validator } from 'vee-validate'
import zh from 'vee-validate/dist/locale/zh_CN';//引入中文文件
import en from 'vee-validate/dist/locale/en.js';//引入英文文件
import * as utils from './utils.js'
import ebus from '../libs/ebus'
// 配置中文
Validator.addLocale(zh);

const config = {
  locale: 'zh_CN',  
  delay: 0,
  fieldsBagName: 'veefields'
};
Vue.use(VeeValidate, config);

// 自定义validate 
const dictionary = {
  zh_CN: {
    messages: {
      email: () => '请输入正确的邮箱格式',
      required: (field) => "请输入" + field
    },
    attributes: {
      email: '邮箱',
      password: '密码',
      name: '名称',
      valiName: '名称',
      valiStart: '开头',
      valiStartPort: '起始端口',
      valiEndPort: '结束端口',
      any: ' ',
      valiminute:'时间',
      valiminute1:'时间',
      valiStartdelaytime: '延迟时间',
      cmd:'动作名',
      ipFormat:'地址',
      duration:'老化时长',
      x:'',
      framerate: '帧率',
    }
  },
  en: {
    messages: {
      email: () => 'Please enter the correct email format',
      required: (field) => "Please input " + field
    },
    attributes: {
      email: 'email',
      password: 'password',
      name: 'name',
      valiName: 'name',
      valiStart: 'Start',
      valiStartPort: 'Start Port',
      valiEndPort: 'End Port',
      any: 'any',
      valiminute:'Time',
      valiminute1:'Time',
      valiStartdelaytime: 'Dalay',
      cmd:'Command',
      ipFormat:'Address',
      duration:'duration',
      x:'',
      framerate: 'framerate',
    }
  },
};

Validator.extend('valiName', {
  messages: {
    zh_CN: field => field + '不能包含特殊字符',
    en: field => field + 'Can not contain special chars',
  },
  validate: value => {
    return !utils.isContainNameSpecial(value)
  }
});
Validator.extend('valiStart', {
  messages: {
    zh_CN: field => field + '不能以数字开头, ' ,
    en: field => field + 'Can\'t starts with number' ,
  },
  validate: value => {
    return utils.isValidStart(value)
  }
});

Validator.extend('valiPort', {
  messages: {
    zh_CN: field => '不在端口范围内',
    en: field => 'Is beyond the scope of port'
  },
  validate: value => {
    return utils.isValidPort(value)
  }
});
Validator.extend('valiminute', {
  messages: {
    zh_CN: field => field + '大于等于30分钟',
    en: field => field + 'Must bigger then 30 minutes'
  },
  validate: value => {
    return utils.isValidAutoCfgTime(value)
  }
});
Validator.extend('ipFormat', {
  messages: {
    zh_CN: field => field + '不正确',
    en: field => field + 'Error'
  },
  validate: value => {
    return utils.isValidIp(value)
  }
});
Validator.extend('duration', {
  messages: {
    zh_CN: field => field + ' 必须在300~432000范围内',
    en: field => field + ' Must In the range of 300 to 432000'
  },
  validate: value => {
    return utils.isValidDuration(value)
  }
});
Validator.extend('framerate', {
  messages: {
    zh_CN: field => '1~60',
    en: field => '1~60',
  },
  validate: value => {
    return (value>=1 && value<=60)
  }
});

Validator.updateDictionary(dictionary);
ebus.on('onChangeLang', (item) => {
  if (item == 'en') {
    Validator.localize('en')
  } else {
    Validator.localize('zh_CN')
  }
  ebus.emit('onReload')
})